<?php
use Illuminate\Database\Capsule\Manager as DB;

#[推客等级管理]
class TuikelevelController extends AcommonController {

    public function init() {
        $this->model = new tuikelevelModel();
        $this->table = 'tuike_level';
        $this->primaryKey = 'level';
        parent::init();
    }

    #[添加推客等级]
    public function addAction()
    {
        if (!$this->isPost()) {
            $table = $this->getTableColumn($this->table);
            $table['award_zhitui'] = 0;
            $table['award_jiantui'] = 0;
            $table['upgrade_rules'] = new \stdClass;
            $this->_view->assign('dataset', json_encode($table));
            $this->_view->display(strtolower($this->controller) . '/edit.html');
        } else {
            if(is_numeric($this->postData['award_zhitui'])){
                if(bccomp($this->postData['award_zhitui'], '100', 3) === 1 || bccomp($this->postData['award_zhitui'], 0.01, 3) === -1){
                    $this->postData['award_zhitui'] = 0;
                }
            }
            if(is_numeric($this->postData['award_jiantui'])){
                if(bccomp($this->postData['award_jiantui'], '100', 3) === 1 || bccomp($this->postData['award_jiantui'], 0.01, 3) === -1){
                    $this->postData['award_jiantui'] = 0;
                }
            }
            $this->postData['upgrade_rules'] = isset($this->postData['upgrade_rules'])? json_encode($this->postData['upgrade_rules'], JSON_UNESCAPED_UNICODE) : json_encode(new \stdClass());
            parent::addAction();
        }
    }

    #[编辑推客等级]
    public function editAction()
    {
        if ($this->isPost()) {
            $input = $this->postData;
            $roles = [
                $this->primaryKey => ['role' => "required|exists:{$this->table}.{$this->primaryKey}", 'msg' => '主键'],
                'name' => ['role' => 'required', 'msg' => '等级名称'],
            ];
            Validate::check($input, $roles);
            $int_level = intval($this->postData['level']);
            if($int_level && is_numeric($this->postData['award_zhitui'])){
                if((round(floatval($this->postData['award_zhitui']), 3) > round(100, 3)) || (round(floatval($this->postData['award_zhitui']), 3) < round(0.01, 3))){
                    $this->postData['award_zhitui'] = 0;
                }
            }
            if($int_level && is_numeric($this->postData['award_jiantui'])){
                if((round(floatval($this->postData['award_jiantui']), 3) > round(100, 3)) || (round(floatval($this->postData['award_jiantui']), 3) < round(0.01, 3))){
                    $this->postData['award_jiantui'] = 0;
                }
            }
            $this->postData['upgrade_rules'] = isset($this->postData['upgrade_rules'])? json_encode($this->postData['upgrade_rules'], JSON_UNESCAPED_UNICODE) : json_encode(new \stdClass());
        }
        parent::editAction();
    }

    #[删除推客等级]
    public function deleteAction()
    {
        $levels = $this->get($this->primaryKey, []);
        if (empty($levels)) {
            ret(2, '参数为空');
        }
        if (is_numeric($levels)) {
            $levels = [$levels];
        }
        if(tuikeModel::query()->whereIn('level', $levels)->exists()){
            ret(2, '删除的等级有推客存在！');
        }
        parent::deleteAction(); // TODO: Change the autogenerated stub
    }

}
